<?php
namespace Amoysea\Controller;
use Think\Controller;
class InvitationController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
    }
    public function index(){
        $m=M('Invitation', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$code=I('code');
		$come_from=I('come_from');		
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($code)){
			$map['code'] = array('like','%'.$code.'%');
        }
		if(!empty($come_from)){
			$map['come_from'] = array('like','%'.$come_from.'%');
        }
		
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//生成邀请码
	function make_code(){
			$m=M('Invitation', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$nums=$param['nums'];
			for($i=0;$i<$nums;$i++){
				$data[$i]['code']=md5(time()+$i);
				$data[$i]['add_time']=time();
			}
			$res=$m->addAll($data);
			if($res){
				$this->success('添加成功');
			}
	}
	//编辑邀请码
	function edit_code(){
			$m=M('Invitation', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$data['come_from']=$param['come_from'];
			$map['id']=$param['id'];
			$res=$m->where($map)->save($data);
			if($res!==false){
				$this->success('编辑成功');
			}
	}
	//删除
	function del(){
		$id=trim(I('id'));
		$m=M('Invitation', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$nums=$m->field('nums')->where($map)->find();
		if($nums['nums']){
			echo "已有注册用户，不能删除！";
			exit;
		}
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//批量删除
	function delAll(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('Invitation', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$list=$m->field('nums')->where($map)->select();
		foreach($list as $k=>$v){
			if($v['nums']){
				$this->error('存在已注册用户的邀请码，不能删除！');
				exit;
			}
		}
		$status=$m->where($map)->delete();
		if($status){
			$this->success('删除成功');
		}else{
			$this->error('出错');
		}
	}
	//设置
	function setStatus(){
		$m=M('Invitation', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$param = I('param.','','trim');
		// dump($param);
		$status=$param['value'];
		$data['status']=$status;
		$ids=$param['ids'];
		$map['id']=array('in',$ids);
		$res=$m->where($map)->save($data);
		if($res!==false){
			$this->success('设置成功');
		}else{
			$this->error('出错');
		}
	}
}